package com.jsoft.jsofted.sys.controller;

import com.diboot.core.service.BaseService;
import com.diboot.core.util.BeanUtils;
import com.jsoft.jsofted.framework.annotation.JwtToken;
import com.jsoft.jsofted.sys.entity.OrgGroupEntity;
import com.jsoft.jsofted.sys.entity.OrgEntity;
import com.jsoft.jsofted.sys.service.GroupService;
import com.jsoft.jsofted.sys.service.OrgService;
import com.jsoft.jsofted.framework.utils.ResponseModel;
import com.jsoft.jsofted.sys.vo.OrgGroupView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/jsofted/group")
public class GroupController {

    @Autowired
    private GroupService groupService;
    @Autowired
    private OrgService orgService;

    @JwtToken
    @RequestMapping("/add")
    public ResponseModel add(@RequestBody OrgGroupView entity) {
        Timestamp time = new Timestamp(System.currentTimeMillis());
        entity.setCreateTime(time);
        entity.setUpdateTime(time);
        entity.setSortValue(new BigDecimal(time.getTime()));

        // 保存机构数据
        OrgEntity orgEntity = new OrgEntity();
        BeanUtils.copyProperties(entity, orgEntity);
        boolean success = orgService.save(orgEntity);

        // 保存公司数据
        OrgGroupEntity groupEntity = new OrgGroupEntity();
        BeanUtils.copyProperties(entity, groupEntity);
        groupEntity.setId(orgEntity.getId());
        success = success && groupService.save(groupEntity);

        // 返回
        if (success) {
            return ResponseModel.makeSuccess(null, groupEntity);
        } else {
            return ResponseModel.makeError(null);
        }
    }


    @JwtToken
    @RequestMapping("/update/{id}")
    public ResponseModel update(
            @PathVariable Long id,
            @RequestBody OrgGroupView entity
    ) {
        entity.setId(id);

        // 保存机构数据
        OrgEntity orgEntity = new OrgEntity();
        BeanUtils.copyProperties(entity, orgEntity);
        boolean success = orgService.updateById(orgEntity);

        // 保存公司数据
        OrgGroupEntity groupEntity = new OrgGroupEntity();
        BeanUtils.copyProperties(entity, groupEntity);
        groupEntity.setId(orgEntity.getId());
        success = success && groupService.updateById(groupEntity);

        // 返回
        if (success) {
            return ResponseModel.makeSuccess(null, groupEntity);
        } else {
            return ResponseModel.makeError(null);
        }
    }

    @JwtToken
    @RequestMapping("/delete/{id}")
    public ResponseModel delete(
            @PathVariable(required = false) String id,
            @RequestBody(required = false) LinkedList<String> idList
    ) {
        boolean success = false;
        if (idList != null && idList.size() > 0) {
            success = orgService.removeByIds(idList);
            success = success && groupService.removeByIds(idList);
        } else if (id != null && id.trim().length() > 0) {
            success = orgService.removeById(id);
            success = success && groupService.removeById(id);
        }

        if (success) {
            return ResponseModel.makeSuccess(null, null);
        } else {
            return ResponseModel.makeError(null);
        }
    }

    @JwtToken
    @RequestMapping("/get/{id}")
    public ResponseModel get(@PathVariable String id) {
        BaseService diOrgService = (BaseService) orgService;
        BaseService diGroupService = (BaseService) groupService;

        OrgEntity orgEntity = (OrgEntity) diOrgService.getViewObject(id, OrgEntity.class);
        OrgGroupEntity groupEntity = (OrgGroupEntity) diGroupService.getViewObject(id, OrgGroupEntity.class);

        OrgGroupView groupBean = new OrgGroupView();
        BeanUtils.copyProperties(groupEntity, groupBean);
        BeanUtils.copyProperties(orgEntity, groupBean);

        if (groupEntity != null) {
            return ResponseModel.makeSuccess(null, groupBean);
        } else {
            return ResponseModel.makeError("数据不存在！");
        }
    }

    @JwtToken
    @RequestMapping("/list")
    public ResponseModel list(@RequestBody(required = false) LinkedHashMap queryParams) {
        String userId = (String)queryParams.get("userId");
        List list = groupService.queryGroupViewByUserId(userId);

        return ResponseModel.makeSuccess(null, list);
    }
}
